var lettersArr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
var tf = ["T", "F"]
var tficon = ["&#x2714;", "&#x2718;"]
var rootnode = ""
var editorTitle = ''
var random = parseInt(Math.random() * 100000000)
var publicAttributeArr = ["a", "b"]
function createTopicCard(param, rootnode) {
    var title = param.title,
        minNum = param.minNum,
        maxNum = param.maxNum,
        perNum = param.perNum,
        rowNum = param.rowNum,
        lineH = param.lineH,
        childrenForEach = param.childrenForEach,
        foreachtype = param.foreachtype,
        way = param.way,
        editor = param.editor,
        totla = param.totla
    //paramObj   参数对象
    var rootnode = document.createElement("div")
    var p = document.createElement("p")
    p.innerHTML = title;
    p.setAttribute("id", "mayEditor-title")
    editorTitle = p
    if (param.lineH) {
        var div = document.createElement("div")
        lineH = 28 * lineH
        div.style.cssText = "width:100%;"
        div.style.height = lineH + "px"
        rootnode.appendChild(div)
        rootnode.insertBefore(p, rootnode.firstElementChild)
        rootnode.classList.add("mainMaxBox")
        document.body.appendChild(rootnode)
        return
    }
    var count = 0
    if (!childrenForEach) {
        if (param.topicLineNum) {
            var createnode = new createNode([minNum, maxNum], "span", "", param.topicLineNum, minNum);
            return createnode.fb(rootnode, p, editor)
        } else {
            if (param.topictype === "composition") {
                var createnode = new createNode([0, totla - 1], "span", "", rowNum);
                 return createnode.fb(rootnode, p, editor, param.topictype)
            } else {
                var fbstr = ''
                switch (rowNum) {
                    case 1:
                        fbstr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        break;
                    case 2:
                        fbstr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        break;
                    case 3:
                        fbstr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        break;
                    case 4:
                        fbstr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        break;
                    case 5:
                        fbstr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        break
                    default:
                        fbstr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                }
                var createnode = new createNode([minNum, maxNum], "u", fbstr, rowNum, minNum);
                createnode.fb(rootnode, p, editor)
            }
        }
    } else {
        if (foreachtype === 'tf' || foreachtype === 'tficon') {
            lettersArr = foreachtype === "tf" ? tf : tficon
        } else if (foreachtype === 'line') {

        } else {
            lettersArr = lettersArr.slice(0, perNum)
        }
        var count = 0;
        var divBox = document.createElement("div")
        random = parseInt(Math.random() * 100000000)
        var topicbox = document.createElement("div")
        for (var i = minNum; i <= maxNum; i++) {
            if (count % rowNum === 0 && !(count < rowNum)) {
                var divBox = document.createElement("div")
                random = parseInt(Math.random() * 100000000)
                if (param.topictype === 'saq') {
                    var createnode = new createNode([0, rowNum], "u", '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', rowNum, i, "saq")
                    divBox.style.width = 90 + "%"
                } else {
                    var createnode = new createNode([0, lettersArr.length], "span", '', rowNum, i, "choice")
                }
                if (way === 1) {
                    divBox.style.cssText = "float:left;margin-left:10px;"
                } else {
                    divBox.style.cssText = "overflow:hidden"
                }
                divBox.appendChild(createnode.choice(way))
                divBox.setAttribute("class", "linebox")
                divBox.classList.add("ckeditor")
            } else {
                if (param.topictype === 'saq') {
                    var createnode = new createNode([0, rowNum], "u", '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', rowNum, i, "saq")
                    divBox.style.width = 90 + "%"
                } else {
                    var createnode = new createNode([0, lettersArr.length], "span", '', rowNum, i, "choice")
                }
                if (way === 1) {
                    divBox.style.cssText = "float:left;margin-left:10px;"
                } else {
                    divBox.style.cssText = "overflow:hidden"
                }
                divBox.appendChild(createnode.choice(way))
                divBox.setAttribute("class", "linebox")
                divBox.classList.add("ckeditor")
            }
            topicbox.appendChild(divBox)
            topicbox.setAttribute("class", 'topicbox')
            rootnode.insertBefore(p, rootnode.firstElementChild)
            rootnode.appendChild(topicbox)
            rootnode.classList.add("mainMaxBox")
            document.body.appendChild(rootnode)
            count++
        }
        if (editor) {
            topicbox.setAttribute("contenteditable", true)
            CKEDITOR.inline(topicbox);
        }
    }
    addEvent()
}

/*
scope                  //循环区间
tag                    //需要创建的最小的元素
str                    //最小元素需要填充的字符
perline                //一行的数量
topicnum               //题号
topicType              //题型
*/

function createNode(scope, tag, str, perline, topicNum, topicType) {
    var astr = str
    var count = 0;
    this.scope = scope
    this.tag = tag
    this.str = str
    this.perline = perline
    this.topicNum = topicNum
    this.topicType = topicType
    //多次循环创建的 比如选择题会创建选项长度
    this.choice = function (paramWay) {
        var div = document.createElement("div");
        topicNum = parseInt(topicNum)
        for (let i = scope[0]; i < scope[1]; i++) {
            if (!(topicType === 'saq')) {
                str = lettersArr[i]
            }
            if (count % perline === 0 && !(count < perline)) {
                var div = document.createElement("div");
                var tagNode = document.createElement(tag);
                if (topicType === 'saq') {
                    tagNode.style.cssText = "display: inline-block;margin:0;padding:0;width:100%"
                    tagNode.innerHTML = str;
                } else {
                    tagNode.innerHTML += str;
                }
                div.appendChild(tagNode)
            } else {
                var tagNode = document.createElement(tag);
                if (topicType === 'saq') {
                    tagNode.innerHTML = str;
                    tagNode.style.cssText = "display: inline-block;margin:0;padding:0;width:100%"
                } else {
                    tagNode.innerHTML += str;
                    tagNode.style.cssText = "font-family: iroomr;"
                }
                div.appendChild(tagNode)
            }
        }
        var span = document.createElement("span")
        span.innerHTML = topicNum + "."
        span.style.cssText = "display:inline-block;width:26px;"
        div.insertBefore(span, div.getElementsByTagName(tag)[0])
        if (paramWay === 0) {
            div.style.cssText = "float:left;margin-left:10px;"
        } else {
            div.style.cssText = "overflow:hidden"
        }
        return div
    }
    //只需创建子元素时
    this.fb = function (RN, pTitle, editor, topictype) {
        var div = document.createElement("div");
        topicNum = parseInt(topicNum)
        for (let i = scope[0]; i <= scope[1]; i++) {
            if (count % perline === 0 && !(count < perline)) {
                var div = document.createElement("div");
                if (topictype) {
                    var span = "<span class='square'></span>"
                    div.setAttribute("class", "squarebox")
                    div.innerHTML += span
                } else {
                    var span = "<span style='display: inline-block;width:26px;'>" + topicNum + "</span>"
                    div.innerHTML += span + fbcreate(tag)
                }
            } else {
                if (topictype) {
                    var span = "<span class='square'></span>"
                    div.setAttribute("class", "squarebox")
                    div.innerHTML += span
                } else {
                    var span = "<span style='display: inline-block;width:26px;'>" + topicNum + "</span>"
                    div.innerHTML += span + fbcreate(tag)
                }
            }
            topicNum++
            RN.appendChild(div)
            count++
        }
        RN.setAttribute("class", "topicbox")
        var mainMaxBox = document.createElement("div")
        mainMaxBox.appendChild(RN)
        mainMaxBox.setAttribute("class", "mainMaxBox")
        mainMaxBox.insertBefore(pTitle, mainMaxBox.firstElementChild)
        if (editor) {
            RN.setAttribute("contenteditable", true)
            CKEDITOR.inline(RN);
        }
        return mainMaxBox

    }
    function fbcreate(tag) {
        var tagNode = document.createElement(tag);
        var tagNodeW = Math.floor(100 / perline)
        tagNode.style.cssText = "display: inline-block;margin:0;padding:0;overflow:hidden"
        tagNode.style.width = tagNodeW - 5 + "%"
        tagNode.innerHTML += str;
        return tagNode.outerHTML
    }
}
//获取元素值
function getNodeval() {
    this.getText = function (node) {
        var val = document.getElementById(node).value
        if (!val && node != "title") {
            return false
        }
        return val
    }
    this.getNum = function (node) {
        var val = document.getElementById(node).value
        if (!val && node != "title") {
            return false
        }
        return parseInt(val)
    }
}
var getnodeval = new getNodeval()
//标题编辑函数
function addEvent() {
    editorTitle.addEventListener("click", function () {
        this.setAttribute("contenteditable", true)
    })
}

/*设置元素属性
obj             设置的目标元素
tyupe           设置的属性名
attributeArr    设置的属性值，为数组格式
*/
function mySetAttribute(obj, type, attributeArr) {
    for (var i = 0; i < attributeArr.length; i++) {
        if (type === "class") {
            obj.classList.add(attributeArr[i])
        } else {
            obj.setAttribute(type, attributeArr[i])
        }

    }
}



function getStyle(node,attr){
    return getComputedStyle(node,null)[attr] ? getComputedStyle(node,null)[att] : node.currentStyle[attr]
}